/*
 * Author: JacksonCoder
 * FileName: PrintingWithYield
 * Version: 1.0.0
 * Date: 2025/2/22
 */

public class PrintingWithYield {
    private static volatile boolean printNumber = true;

    public static void main(String[] args) {
        Thread numberThread = new Thread(new NumberPrinter());
        Thread letterThread = new Thread(new LetterPrinter());

        numberThread.start();
        letterThread.start();

    }

    static class NumberPrinter implements Runnable {

        @Override
        public void run() {
            for (int i = 1; i <= 3; i++) {
                while (!printNumber) {
                    Thread.yield();
                }
                System.out.println(i);
                printNumber = false;
                Thread.yield();
            }
        }
    }

    static class LetterPrinter implements Runnable {

        @Override
        public void run() {
            for (char C = 'A'; C <= 'C'; C++) {
                while (printNumber) {
                    Thread.yield();
                }
                System.out.println(C);
                printNumber = true;
                Thread.yield();
            }
        }
    }

}
